package com.example.tys.baseapplication.View.recycleView;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.RecyclerView;
import android.text.TextPaint;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;

import com.example.tys.baseapplication.R;
import com.example.tys.baseapplication.View.recycleView.sticky.StickyAdapter;

import static android.content.ContentValues.TAG;

/**
 * @author tys
 * @date 2019/2/15
 */

public class TimeLineDividerItemDecoration extends RecyclerView.ItemDecoration {
    private final Context context;

    /**
     * item左边偏移
     */
    private int startOffset;

    /**
     * 时间轴颜色
     */
    private int lineColor = Color.RED;

    protected Paint mPaint;
    protected TextPaint mTextPaint;
    private final StickyAdapter mAdapter;
    private Rect mBounds = new Rect();
    private Drawable mDivider;
    private static final int[] ATTRS = new int[]{android.R.attr.listDivider};

    public TimeLineDividerItemDecoration(Context context, StickyAdapter adapter) {
        this.context = context;
        this.mAdapter = adapter;
        final TypedArray a = context.obtainStyledAttributes(ATTRS);
        mDivider = a.getDrawable(0);
        if (mDivider == null) {
            Log.w(TAG, "@android:attr/listDivider was not set in the theme used for this "
                    + "DividerItemDecoration. Please set that attribute all call setDrawable()");
        }
        a.recycle();
        init();
    }

    protected void init() {
        startOffset = dp2px(75.0f);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStrokeWidth(2.0f);
        mPaint.setStyle(Paint.Style.STROKE);
        mTextPaint = new TextPaint();
        mTextPaint.setAntiAlias(true);
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        outRect.set(startOffset, 0, 0, 0);
    }

    /**
     * 每个item下面的分割线
     *
     * @param canvas
     * @param parent
     * @param state
     * @see DividerItemDecoration#drawVertical(Canvas, RecyclerView)
     */
    @Override
    public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(canvas, parent, state);
        canvas.save();
        final int left;
        final int right;
        //noinspection AndroidLintNewApi - NewApi lint fails to handle overrides.
        if (parent.getClipToPadding()) {
            left = parent.getPaddingLeft();
            right = parent.getWidth() - parent.getPaddingRight();
            canvas.clipRect(left, parent.getPaddingTop(), right,
                    parent.getHeight() - parent.getPaddingBottom());
        } else {
            left = 0;
            right = parent.getWidth();
        }

        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            parent.getDecoratedBoundsWithMargins(child, mBounds);
            final int bottom = mBounds.bottom + Math.round(child.getTranslationY());
            final int top = bottom - mDivider.getIntrinsicHeight();
            mDivider.setBounds(left + startOffset, top, right, bottom);
            mDivider.draw(canvas);
        }
        canvas.restore();
    }

    /**
     * 画左边(时间轴)上的文字或drawable
     *
     * @param canvas
     * @param parent
     * @param state
     */
    @Override
    public void onDrawOver(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
        super.onDrawOver(canvas, parent, state);
        if (mAdapter.isEmpty()) {
            return;
        }
        canvas.save();
        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            parent.getLayoutManager().getDecoratedBoundsWithMargins(child, mBounds);
            //right line
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setColor(lineColor);
            canvas.drawLine(mBounds.left + startOffset, mBounds.top,
                    mBounds.left + startOffset, mBounds.bottom, mPaint);
            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
            int position = layoutParams.getViewLayoutPosition();
            if (!mAdapter.needTitle(position)) {
                continue;
            }
            drawTimeLineDrawable(canvas, position);
            drawTimeLineTitle(canvas, position);
        }
        canvas.restore();
    }

    /**
     * 画时间轴上的drawable
     * @param canvas
     * @param position
     */
    protected void drawTimeLineDrawable(Canvas canvas, int position) {
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.c1);
        Rect rect = new Rect(mBounds.left + startOffset - 20, mBounds.top + dp2px(4),
                mBounds.left + startOffset + 20, mBounds.top + dp2px(4 +
                (mBounds.bottom - mBounds.top) / 5));
        canvas.drawBitmap(bitmap, null, rect, mPaint);
    }

    /**
     * 画时间轴上的文字
     *
     * @param canvas
     * @param position
     */
    protected void drawTimeLineTitle(Canvas canvas, int position) {
        mTextPaint.setColor(Color.BLACK);
        mTextPaint.setTextSize(dp2px(12));
        String itemViewTitle = mAdapter.getItemViewTitle(position);
        canvas.drawText(itemViewTitle,
                mBounds.left + (startOffset - mTextPaint.measureText(itemViewTitle)) / 2, mBounds.top + dp2px(15),
                mTextPaint);

//        mTextPaint.setColor(Color.GRAY);
//        mTextPaint.setTextSize(dp2px(10));
//        canvas.drawText("2019/12/32",
//                mBounds.left + (startOffset - mTextPaint.measureText("2019/12/32")) / 2, mBounds.top + dp2px(24),
//                mTextPaint);
    }


    protected int dp2px(float value) {
        return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value,
                context.getResources().getDisplayMetrics()));
    }

}
